const Router = require('koa-router')
const userValidate = require('@/middlewares/validateMiddlewares/userValidate')
const userController = require('@/controller/userController.js')
const overviewController = require('@/controller/systemOverview')
const userCheckUpdate = require('@/middlewares/checkUpdateMiddlewares/userUpdate')
const userLog = require('@/middlewares/logMiddlewares/userLog')
const { verifyToken } = require('@/utils/jwt')
const { commonSuccessLog } = require('@/middlewares/logMiddlewares/commonLog')
const { verifyAuth } = require('@/middlewares/authMiddlewares/verifyAuth')
const { system } = require('@/config/system')
const { effect } = require('@/config/reactive')

const router = new Router()

/**
 * 用户注册
 */
router.post(
  '/register',
  userValidate.registerValidate,
  userLog.userRegisterLog,
  userController.register
)

/**
 * 用户登录
 */
router.post(
  '/login',
  userValidate.loginValidate,
  userLog.userLoginLog,
  userController.login
)

/**
 * 用户刷新token
 */
router.get('/reflesh', verifyToken(true, true), userController.login)

/**
 * 用户退出登录
 */
router.post(
  '/loginOut',
  verifyToken(),
  userLog.userLogoutLog,
  userController.loginOut
)

/**
 * 检查用户信息
 */
router.post(
  '/checkUserInfo',
  userValidate.checkUserInfoValidate,
  userController.checkUserInfo
)

/**
 * 获取用户个人信息
 */
router.get('/info', verifyToken(), userController.getUserInfo)

/**
 * 更新用户个人信息
 */
router.put(
  '/infoUpdate',
  verifyToken(),
  verifyAuth(['user.editProfile']),
  userValidate.updateUserInfoValidate,
  userCheckUpdate.singerUpdateCheck,
  userLog.updateUserLog,
  userController.updateUserInfo
)

/**
 * 用户找回密码
 */

router.put(
  '/findPwd',
  userValidate.userFindPwdValidate,
  userLog.userFindPwdLog,
  userController.userFindPwd
)

/**
 * 批量启用/禁用用户账号
 */
router.put(
  '/status',
  verifyToken(),
  verifyAuth(['admin.member.edit']),
  userValidate.setUserStatusValidate,
  userLog.setUserStatusLog,
  userController.setUserStatus
)

/**
 * 批量设置用户系统角色
 */
router.put(
  '/setRole',
  verifyToken(),
  verifyAuth(['admin.member.edit']),
  userValidate.setUserRoleValidate,
  userController.setUserRole
)

/**
 * 批量设置用户个人空间大小
 */
router.put(
  '/setSizeMax',
  verifyToken(),
  verifyAuth(['admin.member.edit']),
  userValidate.setUserSizeMaxValidate,
  userController.setUserSizeMax
)

/**
 * 批量删除用户账号
 */
router.delete(
  '/delete',
  userValidate.validateSlideCode(),
  verifyToken(),
  verifyAuth(['admin.member.edit']),
  userCheckUpdate.delUserSave,
  commonSuccessLog,
  userValidate.delUsersValidate,
  userController.delUsers
)

/**
 * 用户列表查询
 */
router.get(
  '/list',
  verifyToken(),
  userValidate.listUsersValidate,
  userController.listUsers
)

/**
 * 查询不属于任何部门的用户
 */
router.get(
  '/notDep/list',
  verifyToken(),
  userValidate.listUsersNotBelongDepValidate,
  userController.listUsersNotBelongDep
)

/**
 * 用户获取自己拥有的权限列表
 */
router.get(
  '/permission/list',
  verifyToken(),
  userController.listUserPermissions
)

/**
 * 用户获取个人配置
 */

router.get('/settings/list', verifyToken(), userController.listUserOption)

/**
 * 用户设置个人配置接口
 */
router.post(
  '/settings/update',
  verifyToken(),
  verifyAuth(['user.editProfile']),
  userValidate.setUserOptionValidate,
  userController.setUserOption
)

/**
 * 用户查询自己的空间使用统计接口
 */
router.get(
  '/settings/spaceChart',
  verifyToken(),
  userValidate.getUserSpaceChart,
  overviewController.listSpaceChart
)

/**
 * 向用户发送验证码
 */
const initSendCodeRouter = () => {
  router.post(
    '/sendCode',
    // 按情况传入true或者false，需要验证图片验证码
    userValidate.validateSlideCode(system.loginValidateCode === '1'),
    userValidate.sendEmailCodeValidate,
    userController.sendEmailCode
  )
}

effect(initSendCodeRouter)

/**
 * 向用户发送滑块验证码
 */
router.get('/slideCode', userController.sendSlideCode)

module.exports = router
